import { request } from '../utils'
import { Toast } from "vant";

/*
GET
  pageNo: 1,
  pageSize: 20,
  keywords: "",
  cid: 0,
  b_id: 0,
  a_id: '1',
  orderKey: 1,
  orderType: 1,
  vshopId: 0,
  sid: 0,
*/
const getProductList = (requestData, cb) => {
  // 获取商品列表 | 商品搜索
  request({
    url: "api/Search/GetSearchProducts",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      let list = res.data.Data.Product
      cb && cb(list)
    } else {
      Toast("获取产品列表失败")
      console.log("获取产品列表失败...")
    }
  })
}

const getAttr = (requestData, cb) => {
  // 获取商品属性
  request({
    url: "api/Search/GetProductAttr",
    params: requestData,
  }).then((res) => {
    if (res.data.Success) {
      let list = res.data.Data
      cb && cb(list)
    } else {
      console.log("获取产品属性失败...")
    }
  })
}

const getActiveProductList = (requestData, cb) => {
  request({
    url: "api/Product/GetHomePageProduct",
    params: requestData,
  }).then((res) => {
    if (res.data.Success) {
      let list = res.data.Data
      // 硬核直降价格处理
      list.cutPriceProduct = list.cutPriceProduct.map(item => {
        item.MinSalePrice = item.MinSalePrice.toFixed(2)
        item.MarketPrice = item.MarketPrice.toFixed(2)
        item.SalePrice = item.SalePrice.toFixed(2)
        item.diffPrice =
          (item.MarketPrice * 100 - item.MinSalePrice * 100) / 100
        item.diffPrice = item.diffPrice.toFixed(2)
        return item
      })

      // 多件多折价格处理
      list.manyPriceProduct = list.manyPriceProduct.map((item) => {
        item.MinSalePrice = item.MinSalePrice.toFixed(2)
        item.MarketPrice = item.MarketPrice.toFixed(2)
        item.SalePrice = item.SalePrice.toFixed(2)
        return item
      })
      
      cb && cb(list)
    } else {
      Toast("获取产品列表失败")
      console.log("获取产品列表失败...")
    }
  })
}

export { getProductList, getActiveProductList, getAttr }